package com.io;

import java.io.*;

/**
 * 通过BufferedReader将案例4中写入的内容读出来，显示到控制台上。
 *
 * @author: Jovi
 * @createTime: 2017-06-09 15:03
 **/
public class BufferReaderAndWriterTest {

    public static void main(String[] args) {
        readerTest();

    }

    private static void writerTest(){
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
             fw = new FileWriter("b.txt");
             bw = new BufferedWriter(fw);
            bw.write("nihao");
            bw.newLine();
            bw.write("哈哈");
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private static void readerTest() {
        FileReader fr = null;
        BufferedReader br = null;

        try {
            fr = new FileReader("b.txt");
            br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("Read over");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
